package com.rimi.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.rimi.bean.PageBean;
import com.rimi.bean.RoleBean;
import com.rimi.bean.UserBean;
import com.rimi.service.RoleService;
import com.rimi.service.UserService;

@Controller
@SessionAttributes("ubb")
public class UserController {
	@Autowired
	private UserService us;
	@Autowired
	private RoleService rs;

	@RequestMapping("userInfo")
	public String userInfo(Model m, PageBean pb, UserBean ub, HttpSession session) {

		// 1.此条件用于判断是否是第一次进入查询,第一次进入查询,ub是框架封装的一个对象,因为前页面没有合适的字段属性全都为null
		// 2.当ub.getUserName() == null而且session.getAttribute("ubb")= null说明是第一次进入查询
		// 3.当ub.getUserName() == null && session.getAttribute("ubb") !=
		// null说明已经不是第一次查询,而且是页面
		// 过来的时候没有点查询(应该是上下页),因为这时候封装的ub对象没有相应字段.ub要 被重新赋值

		// 此条件用于判断是否是第一次进入查询

		if (ub.getUserName() == null && session.getAttribute("ubb") != null) {
			ub = (UserBean) session.getAttribute("ubb");
		}
		Map map = new HashMap();
		List li = us.userInfo(ub);
		if (pb != null) {
			pb.setCount(li.size());
		}
		map.put("pb", pb);
		map.put("ubb", ub);
		List<UserBean> list = us.userInfoZero(map);
		List<RoleBean> list2 = new ArrayList();
		for (int i = 0; i < list.size(); i++) {
			boolean flag = true;
			RoleBean r = rs.getRoleById(list.get(i).getRb().getRoleId());
			if (list2.size() == 0) {
				list2.add(r);
			} else {
				for (int j = 0; j < list2.size(); j++) {

					if (r.getRoleName().equals(list2.get(j).getRoleName())) {
						flag = false;
						break;
					}
				}
				if (flag) {
					list2.add(r);
				}
			}

		}
		m.addAttribute("ubb", ub);
		m.addAttribute("pb", pb);
		m.addAttribute("list", list);
		m.addAttribute("list2", list2);
		return "User/index";
	}

	@RequestMapping("checkPassword")
	public void checkPassword(HttpSession session, String code, HttpServletResponse response,
			HttpServletRequest request) {
		// UserBean ub = (UserBean) session.getAttribute("ub");
		session = request.getSession();
		String code1 = request.getParameter("code");
		String p = (String) session.getAttribute("ps");
		PrintWriter pt;
		try {
			pt = response.getWriter();
			if (p.equals(code1)) {
				pt.print(0);
				pt.flush();
				pt.close();
			} else {
				pt.print(1);
				pt.flush();
				pt.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@RequestMapping("passwordInfo")
	public String passwordInfo() {

		return "Password/add";
	}

	@RequestMapping("editPs")
	public String editPs(String newPasswordd, HttpSession session) {
		UserBean ub = (UserBean) session.getAttribute("ub");
		int id = ub.getUserId();
		UserBean u = new UserBean();
		u.setUserId(id);
		u.setUserPassword(newPasswordd);
		us.editPs(u);
		return "redirect:index.jsp";
	}
}
